package com.nativetalk.service.course.update;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.course.teacher.TdNtTeachCourse;
import com.nativetalk.bean.course.teacher.TdNtTeachCourseInsert;
import com.nativetalk.bean.teacher.TdNtTeacherIndex;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.dao.TdNtTeachCourseDao;
import com.nativetalk.dao.TdNtTeacherDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by lizhun on 16/1/11.
 */
@Service
public class CourseUpdateServiceImpl implements CourseUpdateService {
    private static Logger logger = Logger.getLogger(CourseUpdateServiceImpl.class);
    @Resource
    private TdNtTeachCourseDao tdNtTeachCourseDao;
    @Resource
    private TdNtTeacherDao tdNtTeacherDao;

    @Override
    public RetInfo openCourse(int cou_id, int tea_id, int tc_state) {
        String logInfo = this.getClass().getName() + ":openCourse:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            TdNtTeacherIndex tdNtTeacherState = tdNtTeacherDao.findTdNtTeacherIndex(tea_id);
            if (tdNtTeacherState.getTea_islock().equals("1")) {

                Map<String, Object> search = new HashMap<>();
                search.put("cou_id", cou_id);
                search.put("tea_id", tea_id);
                TdNtTeachCourse tdNtTeachCourse = tdNtTeachCourseDao.findTdNtTeachCourseByCouIdAndTeaId(search);
                if (tdNtTeachCourse != null) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("cou_id", cou_id);
                    map.put("tea_id", tea_id);
                    map.put("tc_state", tc_state);
                    tdNtTeachCourseDao.updateTdNtTeachCourse(map);
                    retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                    retInfo.setTip("更新成功");
                } else {
                    retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                    retInfo.setTip(ValidationMessages.getValue("TEACH.COURSE.RESLUT.ERROR"));
                }


            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip(ValidationMessages.getValue("TEACH.COURSE.RESLUT.NULL"));
            }

        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(), e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo addTeachCoures(TdNtTeachCourseInsert tdNtTeachCourseInsert) {
        String logInfo = this.getClass().getName() + ":addTeachCoures:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, Object> search = new HashMap<>();
            search.put("cou_id", tdNtTeachCourseInsert.getCou_id());
            search.put("tea_id", tdNtTeachCourseInsert.getTea_id());
            TdNtTeachCourse tdNtTeachCourse = tdNtTeachCourseDao.findTdNtTeachCourseByCouIdAndTeaId(search);//查询老师开设的某门课程
            if (tdNtTeachCourse != null) {
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("Unclock success.");
            } else {
                tdNtTeachCourseInsert.setCou_amount(new BigDecimal("2"));
                tdNtTeachCourseInsert.setTc_state("2");//1为解锁2开启3关闭,当老师观看完开课视频后默认为开启教学状态  （2018-05-03变更，以前默认为关闭教学）
                tdNtTeachCourseDao.addTdNtTeachCourse(tdNtTeachCourseInsert);
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("Unclock success.");
            }
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(), e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
